package com.liam.chinesevietnamesecorpus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liam.chinesevietnamesecorpus.common.BaseController;
import com.liam.chinesevietnamesecorpus.common.R;
import com.liam.chinesevietnamesecorpus.dto.EmailVerificationDTO;
import com.liam.chinesevietnamesecorpus.dto.LoginDTO;
import com.liam.chinesevietnamesecorpus.dto.UserDTO;
import com.liam.chinesevietnamesecorpus.dto.UserUpdateDTO;
import com.liam.chinesevietnamesecorpus.service.EmailService;
import com.liam.chinesevietnamesecorpus.service.UserService;
import com.liam.chinesevietnamesecorpus.vo.LoginVO;
import com.liam.chinesevietnamesecorpus.vo.UserVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 * 
 * @author Liam
 */
@Slf4j
@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private EmailService emailService;

    /**
     * 发送邮箱验证码
     */
    @PostMapping("/auth/send-verification-code")
    public R<Void> sendVerificationCode(@Valid @RequestBody EmailVerificationDTO emailVerificationDTO) {
        log.info("发送邮箱验证码请求：email={}, type={}", emailVerificationDTO.getEmail(), emailVerificationDTO.getType());
        emailService.sendVerificationCode(emailVerificationDTO.getEmail(), emailVerificationDTO.getType());
        log.info("邮箱验证码发送成功：email={}", emailVerificationDTO.getEmail());
        return R.ok("验证码发送成功，请查收邮件");
    }

    /**
     * 用户登录
     */
    @PostMapping("/auth/login")
    public R<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) {
        log.info("用户登录请求：{}", loginDTO.getUsername());
        LoginVO loginVO = userService.login(loginDTO);
        log.info("用户登录成功：{}", loginDTO.getUsername());
        return R.ok("登录成功", loginVO);
    }

    /**
     * 用户注册
     */
    @PostMapping("/auth/register")
    public R<UserVO> register(@Valid @RequestBody UserDTO userDTO) {
        log.info("用户注册请求：{}", userDTO.getUsername());
        UserVO userVO = userService.register(userDTO);
        log.info("用户注册成功：{}", userDTO.getUsername());
        return R.ok("注册成功", userVO);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/user/profile/{id}")
    public R<UserVO> getUserProfile(@PathVariable Long id) {
        UserVO userVO = userService.getUserById(id);
        return R.ok(userVO);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/user/profile/{id}")
    public R<UserVO> updateUserProfile(@PathVariable Long id, @Valid @RequestBody UserUpdateDTO userUpdateDTO) {
        log.info("更新用户信息请求：用户ID={}", id);
        UserVO userVO = userService.updateUser(id, userUpdateDTO);
        log.info("用户信息更新成功：用户ID={}", id);
        return R.ok("更新成功", userVO);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/user/{id}")
    public R<Void> deleteUser(@PathVariable Long id) {
        log.info("删除用户请求：用户ID={}", id);
        userService.deleteUser(id);
        log.info("用户删除成功：用户ID={}", id);
        return R.ok("删除成功");
    }


    /**
     * 用户登出
     */
    @PostMapping("/auth/logout")
    public R<Void> logout() {
        // 由于使用JWT，登出主要在前端清除token
        // 这里可以记录登出日志或加入黑名单机制
        log.info("用户登出请求");
        return R.ok("登出成功");
    }
}
